<template>
  <div :class="className" :style="{height:height,width:width}" />
</template>

<script>
import echarts from 'echarts'
require('echarts/theme/macarons') // echarts theme
import resize from './mixins/resize'

const animationDuration = 1000

export default {
  mixins: [resize],
  props: {
    className: {
      type: String,
      default: 'chart'
    },
    width: {
      type: String,
      default: '100%'
    },
    height: {
      type: String,
      default: '100%'
    },
    legendData: {
      type: Array,
      default: () => {
        return ['pageA', 'pageB', 'pageC']
      }
    },
    xAxisData: {
      type: Array,
      default: () => {
        return ['Mon', 'Tue', 'Wed', 'Thu', 'Fri', 'Sat', 'Sun']
      }
    },
    seriesData: {
      type: Array,
      default: () => {
        return [{
          name: 'pageA',
          type: 'bar',
          stack: 'vistors',
          barWidth: '60%',
          data: [79, 52, 200, 334, 390, 330, 220],
          animationDuration
        }, {
          name: 'pageB',
          type: 'bar',
          stack: 'vistors',
          barWidth: '60%',
          data: [80, 52, 200, 334, 390, 330, 220],
          animationDuration
        }, {
          name: 'pageC',
          type: 'bar',
          stack: 'vistors',
          barWidth: '60%',
          data: [30, 52, 200, 334, 390, 330, 220],
          animationDuration
        }]
      }
    },
    stack: {
      type: Boolean,
      default: () => {
        return true
      }
    }
  },
  data() {
    return {
      chart: null
    }
  },
  watch: {
    // seriesData 是数据 监听数据变化的时候 触发setOptions  修改数据
    seriesData(val) {
      this.$nextTick(() => {
        this.setOptions()
      })
    }
  },
  mounted() {
    this.$nextTick(() => {
      this.initChart()
    })
  },
  beforeDestroy() {
    if (!this.chart) {
      return
    }
    this.chart.dispose()
    this.chart = null
  },
  methods: {
    initChart() {
      this.chart = echarts.init(this.$el, 'macarons')
      this.setOptions()
    },

    setOptions() {
      const { legendData, seriesData, xAxisData, stack } = this

      // 组装数据
      const data = []
      seriesData.forEach((el, index) => {
        data.push({
          name: el.name,
          type: 'bar',
          stack: stack ? 'vistors' : index,
          barWidth: el.barWidth || '60%',
          data: el.data,
          animationDuration
        })
      })

      this.chart.setOption({
        tooltip: {
          trigger: 'axis',
          axisPointer: { // 坐标轴指示器，坐标轴触发有效
            type: 'shadow' // 默认为直线，可选为：'line' | 'shadow'
          }
        },
        grid: {
          top: 50,
          left: '2%',
          right: '2%',
          bottom: '5%',
          containLabel: true
        },
        legend: {
          // x: '5%',
          top: '2%',
          left: 'right',
          textStyle: {
            color: '#90979c'
          },
          data: legendData
        },
        xAxis: [{
          type: 'category',
          data: xAxisData,
          axisLine: {
            lineStyle: {
              color: '#57617B'
            }
          },
          axisTick: {
            alignWithLabel: true
          }
        }],
        yAxis: [{
          type: 'value',
          axisLine: {
            lineStyle: {
              color: '#57617B'
            }
          },
          axisTick: {
            show: false
          }
        }],
        series: data
      })
    }
  }
}
</script>
